package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Article;
import com.example.demo.entity.Role;
import com.example.demo.service.impl.ArticleServiceImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wzz
 * @since 2023-09-24
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleServiceImpl articleServiceImpl;
    @PostMapping("/add")
    public Result add(@RequestBody Article article){
        return Result.success(articleServiceImpl.saveOrUpdate(article));
    }

    @GetMapping("/list")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam String name
    ){
        IPage<Article> page = new Page<>(pageNum,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(name)){
            queryWrapper.like("name",name);
        }
        return Result.success(articleServiceImpl.page(page,queryWrapper));
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable String id){
        return Result.success(articleServiceImpl.getById(id));
    };

    @DeleteMapping("/del/{id}")
    public Result deleteRole(@PathVariable String id){
        return Result.success(articleServiceImpl.removeById(id));
    }

}
